package com.huawei.appgallery.foundation.ui.framework.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import java.util.ArrayList;
import java.util.List;
import o.cjx;
import o.MinimumFactory;

public class MultiLineLabelLayout extends FrameLayout {
    private List<cjx> ˊ;
    private int ˋ = 1000;
    private int ˎ = 0;
    public int ˏ;
    private int ॱ = -1;
    private d ॱॱ = null;

    public interface d {
        void ˏ(int i);
    }

    public MultiLineLabelLayout(Context context) {
        super(context);
    }

    public MultiLineLabelLayout(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
    }

    public MultiLineLabelLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
    }

    public void setMaxLine(int i) {
        this.ˋ = i;
    }

    public void setLayoutListener(d dVar) {
        this.ॱॱ = dVar;
    }

    public int ˋ() {
        return this.ॱ;
    }

    public void setFirstRowTopMargin(int i) {
        this.ॱ = i;
    }

    protected void onMeasure(int i, int i2) {
        super.onMeasure(i, i2);
        if (getChildCount() > 0) {
            int measuredWidth = getMeasuredWidth();
            setMeasuredDimension(measuredWidth, ˊ(measuredWidth));
        }
    }

    private int ˊ(int i) {
        this.ˊ = new ArrayList();
        cjx o_cjx = new cjx();
        this.ˊ.add(o_cjx);
        ˋ(i, o_cjx);
        this.ˎ = 0;
        int i2 = 0;
        int i3 = 0;
        while (i2 < this.ˊ.size() && i2 < this.ˋ) {
            i3 += ((cjx) this.ˊ.get(i2)).ˋ;
            this.ˎ = ((cjx) this.ˊ.get(i2)).ˎ + this.ˎ;
            i2++;
        }
        return i3;
    }

    private void ˋ(int i, cjx o_cjx) {
        int i2 = 0;
        for (int i3 = 0; i3 < getChildCount(); i3++) {
            int i4;
            View childAt = getChildAt(i3);
            int measuredWidth = childAt.getMeasuredWidth();
            int measuredHeight = childAt.getMeasuredHeight();
            LayoutParams layoutParams = childAt.getLayoutParams();
            boolean z = layoutParams instanceof FrameLayout.LayoutParams;
            if (z) {
                measuredWidth += this.ˏ;
            }
            if (measuredWidth > i) {
                measuredWidth = i;
            }
            i2 += measuredWidth;
            if (i2 > i) {
                if (i2 - this.ˏ <= i) {
                    i2 -= this.ˏ;
                } else {
                    o_cjx = new cjx();
                    this.ˊ.add(o_cjx);
                    i2 = measuredWidth;
                }
            }
            if (z) {
                FrameLayout.LayoutParams layoutParams2 = (FrameLayout.LayoutParams) layoutParams;
                if (ˋ() == -1 || this.ˊ.size() != 1) {
                    i4 = layoutParams2.bottomMargin + (measuredHeight + layoutParams2.topMargin);
                } else {
                    i4 = layoutParams2.bottomMargin + (measuredHeight + ˋ());
                }
            } else {
                i4 = measuredHeight;
            }
            if (i4 > o_cjx.ˋ) {
                o_cjx.ˋ = i4;
            }
            o_cjx.ˎ++;
        }
    }

    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        if (getChildCount() <= 0) {
            super.onLayout(z, i, i2, i3, i4);
        } else {
            ˎ();
        }
    }

    private void ˎ() {
        int measuredWidth = getMeasuredWidth();
        int i = 0;
        int i2 = 0;
        cjx o_cjx = (cjx) this.ˊ.get(0);
        int i3 = 0;
        int i4 = 0;
        while (i < getChildCount()) {
            int i5;
            cjx o_cjx2;
            int i6;
            int i7;
            int i8 = this.ˏ;
            View childAt = getChildAt(i);
            FrameLayout.LayoutParams ˋ = ˋ(childAt);
            int measuredWidth2 = childAt.getMeasuredWidth();
            int measuredHeight = childAt.getMeasuredHeight();
            measuredWidth2 += this.ˏ;
            if (measuredWidth2 > measuredWidth) {
                measuredWidth2 = measuredWidth;
            }
            i4 += measuredWidth2;
            if (i4 <= measuredWidth) {
                i5 = i8;
                i8 = i3;
                o_cjx2 = o_cjx;
                i6 = i2;
                i2 = i4;
                i4 = i5;
            } else if (i4 - this.ˏ <= measuredWidth) {
                i8 = i4 - this.ˏ;
                measuredWidth2 -= this.ˏ;
                i4 = 0;
                i5 = i8;
                i8 = i3;
                o_cjx2 = o_cjx;
                i6 = i2;
                i2 = i5;
            } else {
                i3++;
                i6 = i2 + o_cjx.ˋ;
                i2 = measuredWidth2 + 0;
                i5 = i8;
                i8 = i3;
                o_cjx2 = (cjx) this.ˊ.get(i3);
                i4 = i5;
            }
            if (MinimumFactory.getBoolValue(getContext())) {
                measuredWidth2 += measuredWidth - i2;
                i4 = measuredWidth - i2;
            } else {
                i5 = i2 - measuredWidth2;
                measuredWidth2 = i2 - i4;
                i4 = i5;
            }
            if (ˋ() == -1 || i8 != 0) {
                i7 = ˋ.topMargin + i6;
            } else {
                i7 = ˋ();
            }
            measuredHeight += i7;
            if (i8 >= this.ˋ) {
                break;
            }
            childAt.layout(i4, i7, measuredWidth2, measuredHeight);
            i++;
            i4 = i2;
            i2 = i6;
            o_cjx = o_cjx2;
            i3 = i8;
        }
        ˏ();
    }

    private void ˏ() {
        if (this.ॱॱ != null && this.ˎ > 0 && getChildCount() > 0) {
            this.ॱॱ.ˏ(this.ˎ);
        }
    }

    private FrameLayout.LayoutParams ˋ(View view) {
        LayoutParams layoutParams = view.getLayoutParams();
        if (layoutParams == null || !(layoutParams instanceof FrameLayout.LayoutParams)) {
            return new FrameLayout.LayoutParams(view.getWidth(), view.getHeight());
        }
        return (FrameLayout.LayoutParams) layoutParams;
    }
}
